//
//  AIPersonalInfoController.m
//  Mia
//
//  Created by Vill on 2024/1/15.
//

#import "AIPersonalInfoController.h"
#import "AIPersonalListCell.h"      //列表
#import "AIPersonalGenderView.h"    //genderview
#import "AIPersonalMaritalView.h"   //MaritalView

@interface AIPersonalInfoController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *doneBtn;
@property (nonatomic, strong) UILabel *feedTitle;

@property (nonatomic, strong) UIScrollView *containScrollerView;  // 容器
@property (nonatomic, strong) UIView *nameContain;     //容器
@property (nonatomic, strong) UITextField *nameTxt;    //昵称

@property (nonatomic, strong) AIPersonalListCell *birthView; // 生日
@property (nonatomic, strong) UILabel *sexTitle;
@property (nonatomic, strong) AIPersonalGenderView *genderView; //性别
@property (nonatomic, strong) AIPersonalListCell *educationView; // 学历
@property (nonatomic, strong) AIPersonalListCell *peofessionView; // 职业

@property (nonatomic, strong) UILabel *mariTitle;
@property (nonatomic, strong) AIPersonalMaritalView *maritalView; //婚姻状况


@end

@implementation AIPersonalInfoController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated {
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorHex:@"#F5F5F5"];
    
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.feedTitle];
    [self.view addSubview:self.doneBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(44));
        make.leading.mas_equalTo(self.view).mas_offset(PTW(10));
        make.top.mas_equalTo(self.view).mas_offset(KStatusBarHeight);
    }];
    [self.feedTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.backBtn);
    }];
    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(PTW(50), 44));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(10));
        make.centerY.mas_equalTo(self.backBtn);
    }];
    [self.view addSubview:self.containScrollerView];
    [self.containScrollerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backBtn.mas_bottom);
    }];
    
    [self.containScrollerView addSubview:self.nameContain];
    [self.nameContain addSubview:self.nameTxt];
    [self.nameContain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).mas_offset(PTW(15));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(15));
        make.height.mas_equalTo(PTW(60));
        make.top.mas_equalTo(self.containScrollerView).mas_offset(PTW(30));
    }];
    [self.nameTxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameContain).mas_offset(PTW(25));
        make.top.bottom.mas_equalTo(self.nameContain);
        make.trailing.mas_equalTo(self.nameContain).mas_offset(-PTW(20));
    }];
    
    [self.containScrollerView addSubview:self.birthView];
    [self.containScrollerView addSubview:self.sexTitle];
    [self.containScrollerView addSubview:self.genderView];
    [self.containScrollerView addSubview:self.educationView];
    [self.containScrollerView addSubview:self.peofessionView];
    
    [self.containScrollerView addSubview:self.mariTitle];
    [self.containScrollerView addSubview:self.maritalView];
    
    [self.birthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.nameContain.mas_bottom).mas_offset(PTW(20));
    }];
    [self.sexTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.birthView.mas_bottom).mas_offset(PTW(25));
    }];
    [self.genderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.sexTitle.mas_bottom).mas_offset(PTW(16));
        make.height.mas_equalTo(PTW(60));
    }];
    [self.educationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.genderView.mas_bottom).mas_offset(PTW(20));
    }];
    [self.peofessionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.educationView.mas_bottom).mas_offset(PTW(20));
    }];
    
    [self.mariTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.peofessionView.mas_bottom).mas_offset(PTW(25));
    }];
    [self.maritalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.nameContain);
        make.top.mas_equalTo(self.mariTitle.mas_bottom).mas_offset(PTW(16));
        make.height.mas_equalTo(PTW(125));
    }];
    
    self.containScrollerView.contentSize = CGSizeMake(KScreenWidth,PTW(650));
    
}

-(void)doneAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.birthView.bgButton.selected = NO;
        self.birthView.arrowImg.transform = CGAffineTransformIdentity;
    }];
}

#pragma mark -------------- lazy -----------
- (UIButton *)backBtn{
    if(!_backBtn){
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:KImaged(@"ai_setting_back") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(fanhuiAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)fanhuiAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UILabel *)feedTitle{
    if (!_feedTitle){
        _feedTitle = [UILabel new];
        _feedTitle.text = @"Personal information";
        _feedTitle.font = [UIFont fontWithWeight:Medium size:PTW(17)];
        _feedTitle.textColor = [UIColor colorHex:@"#333333"];
    }
    return _feedTitle;
}

- (UIButton *)doneBtn{
    if(!_doneBtn){
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn setTitle:@"Done" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor colorHex:@"#333333"] forState:UIControlStateNormal];
        _doneBtn.titleLabel.font = [UIFont fontWithWeight:Regular size:PTW(15)];
        [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}

- (UIScrollView *)containScrollerView{
    if(!_containScrollerView){
        _containScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, KNavigationBarHeight + PTW(10), KScreenWidth, KScreenHeight - KNavigationBarHeight - PTW(10))];
        _containScrollerView.backgroundColor = UIColor.clearColor;
        _containScrollerView.showsVerticalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _containScrollerView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _containScrollerView;
}

- (UIView *)nameContain{
    if(!_nameContain){
        _nameContain = [[UIView alloc] init];
        _nameContain.layer.cornerRadius = PTW(10);
        _nameContain.layer.masksToBounds = YES;
        _nameContain.backgroundColor = UIColor.whiteColor;
    }
    return _nameContain;
}

- (UITextField *)nameTxt{
    if(!_nameTxt){
        _nameTxt = [UITextField new];
        _nameTxt.placeholder = @"Your Name";
        _nameTxt.placeholderColor = [UIColor colorHex:@"#333333" alpha:0.5];
        _nameTxt.textColor = [UIColor colorHex:@"#333333"];
        [_nameTxt setModifyClearButton:@"ai_textfield_close"];
        _nameTxt.delegate = self;
    }
    return _nameTxt;
}

// 代理方法
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length - range.length + string.length > 30){
        return NO;
    }
    return YES;
}

- (AIPersonalListCell *)birthView{
    if(!_birthView){
        _birthView = [[AIPersonalListCell alloc] init];
        AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
        if ([model.birthday strHasValue]){
            _birthView.titleLbl.text = model.birthday;
            _birthView.titleLbl.textColor = [UIColor colorHex:@"#333333"];
        }else{
            _birthView.titleLbl.text = @"Your date of birth";
            _birthView.titleLbl.textColor = [UIColor colorHex:@"#333333" alpha:0.5];
        }
    }
    return _birthView;
}

- (UILabel *)sexTitle{
    if(!_sexTitle){
        _sexTitle = [UILabel new];
        _sexTitle.text = @"Your Gender";
        _sexTitle.font = [UIFont fontWithWeight:Regular size:PTW(15)];
        _sexTitle.textColor = [UIColor colorHex:@"#333333" alpha:0.5];
    }
    return _sexTitle;
}

- (AIPersonalGenderView *)genderView{
    if(!_genderView){
        _genderView = [[AIPersonalGenderView alloc] init];
    }
    return _genderView;
}

- (AIPersonalListCell *)educationView{
    if(!_educationView){
        _educationView = [[AIPersonalListCell alloc] init];
        AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
        if ([model.birthday strHasValue]){
            _educationView.titleLbl.text = model.birthday;
            _educationView.titleLbl.textColor = [UIColor colorHex:@"#333333"];
        }else{
            _educationView.titleLbl.text = @"Education level";
            _educationView.titleLbl.textColor = [UIColor colorHex:@"#333333" alpha:0.5];
        }
    }
    return _educationView;
}

- (AIPersonalListCell *)peofessionView{
    if(!_peofessionView){
        _peofessionView = [[AIPersonalListCell alloc] init];
        AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
        if ([model.birthday strHasValue]){
            _peofessionView.titleLbl.text = model.birthday;
            _peofessionView.titleLbl.textColor = [UIColor colorHex:@"#333333"];
        }else{
            _peofessionView.titleLbl.text = @"Profession";
            _peofessionView.titleLbl.textColor = [UIColor colorHex:@"#333333" alpha:0.5];
        }
    }
    return _peofessionView;
}

- (UILabel *)mariTitle{
    if(!_mariTitle){
        _mariTitle = [UILabel new];
        _mariTitle.text = @"Marital status";
        _mariTitle.font = [UIFont fontWithWeight:Regular size:PTW(15)];
        _mariTitle.textColor = [UIColor colorHex:@"#333333" alpha:0.5];
    }
    return _mariTitle;
}

- (AIPersonalMaritalView *)maritalView{
    if(!_maritalView){
        _maritalView = [[AIPersonalMaritalView alloc] init];
    }
    return _maritalView;
}

@end
